package vip.liux.front.adapter.web;

import org.springframework.web.bind.annotation.*;
import vip.liux.application.dots.ListResultDto;
import vip.liux.contracts.shared.enums.coupon.UsedTag;
import vip.liux.core.current.ICurrentUser;
import vip.liux.front.application.service.coupon.ShopCouponDto;
import vip.liux.front.application.service.coupon.UserCouponDto;
import vip.liux.front.application.service.coupon.UserCouponService;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/user-coupons")
public class UserCouponController {

    private final UserCouponService userCouponService;
    private final ICurrentUser<Long> currentUser;

    public UserCouponController(UserCouponService userCouponService, ICurrentUser<Long> currentUser) {
        this.userCouponService = userCouponService;
        this.currentUser = currentUser;
    }


    @GetMapping("/list")
    public ListResultDto<ShopCouponDto> queryAvailableCoupons() {
        return userCouponService.queryAvailableCoupons(Objects.requireNonNull(currentUser.getId()));
    }

    /**
     * 领取优惠券
     *
     * @param input 优惠券编号
     */
    @PostMapping("/receive")
    public void receiveCoupon(@RequestBody ReceiveCouponDto input) {
        userCouponService.receiveCoupon(input.couponNo(), Objects.requireNonNull(currentUser.getId()));
    }

    /**
     * 查询用户优惠券
     *
     * @return 用户优惠券数据传输对象
     */
    @GetMapping("/my")
    public List<UserCouponDto> my(@RequestParam UsedTag type) {
        return userCouponService.query(Objects.requireNonNull(currentUser.getId()), type);
    }

    public record ReceiveCouponDto(String couponNo) {
    }

}